package resultutil

type Response struct {
	Code int `json:"code"`
	Msg string `json:"msg"`
	Data interface{} `json:"data"`
	IsSuccess bool `json:"isSuccess"`
}

func Success() Response {
	return genResponse(&SuccessCode, nil)
}

func SuccessWithMsg(msg string) Response {
	suc := SuccessCode
	if msg != "" {
		suc.msg = msg
	}
	return genResponse(&suc, nil)
}

func SuccessWithData(data interface{}) Response {
	return genResponse(&SuccessCode, data)
}

func SuccessWithDataMsg(data interface{}, msg string) Response {
	suc := SuccessCode
	if msg != "" {
		suc.msg = msg
	}
	return genResponse(&suc, data)
}

func Error(code ErrorCode) Response {
	return genResponse(&code, nil)
}

func ErrorWithMsg(code ErrorCode, msg string) Response {
	co := code
	if msg != "" {
		co.msg  = msg
	}
	return genResponse(&co, nil)
}

func genResponse(code *ErrorCode, data interface{}) Response {
	return Response{
		Code:      code.code,
		Msg:       code.msg,
		Data:      data,
		IsSuccess: code.isSuccess,
	}
}